package com.yqfk.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqfk.auto.JSONAuthentication;
import com.yqfk.entity.YNotice;
import com.yqfk.entity.YPun;
import com.yqfk.mapper.YNoticeMapper;
import com.yqfk.response.Result;
import com.yqfk.utils.UpPhotoNameUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.MultipartFilter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yqfk
 * @since 2022-03-30
 */
@RestController
@RequestMapping("/y-notice")
public class YNoticeController extends JSONAuthentication {
    @Autowired
    YNoticeMapper yNoticeMapper;
    @ApiOperation(value = "添加公告")
    @PostMapping("/add")
    public void addCar(@RequestParam("file") MultipartFile file, HttpServletRequest request, HttpServletResponse response, @RequestParam("value") String value,@RequestParam("title") String title) throws Exception {
       YNotice yNotice=new YNotice();
       yNotice.setValue(value);
       yNotice.setTitle(title);
        byte[] bytes = file.getBytes();
        String imageFileName = file.getOriginalFilename();
        String fileName = UpPhotoNameUtils.getPhotoName("img",imageFileName);
        Path path = Paths.get("D:\\自己的项目\\服装租赁系统\\clothing\\src\\assets\\picture\\" + fileName);
        //“C:\\框架\\D4\\d4_pc_ui\\src\\assets\\images\\img\\”为本地目录
        Files.write(path, bytes);//写入文件
        String avatar_url=fileName;
        yNotice.setPicture(avatar_url);
        response.reset();
        if (yNoticeMapper.insert(yNotice) > 0) {
            Result ok = Result.ok().message("添加公告成功");
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "删除公告")
    @PostMapping("/del")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id) throws Exception {
        int i = yNoticeMapper.deleteById(id);
        if (i > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "修改公告")
    @PostMapping("/upd")
    public void upd(HttpServletRequest request, HttpServletResponse response, @RequestBody YNotice yNotice) throws Exception {
        if (yNoticeMapper.updateById(yNotice) > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查询全部公告")
    @PostMapping("/findAllNotice")
    public void findAllNotice(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size, @RequestParam("current") int current) throws Exception {
        Page<YNotice> shUserPage = new Page<>(current, size);
        Page<YNotice> shUserPages = yNoticeMapper.selectPage(shUserPage, null);
        List<YNotice> records = shUserPages.getRecords();
        int total = yNoticeMapper.selectCount(null);
        if (records.size() > 0) {
            Result ok = Result.ok().data("notice", records).data("total", total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "根据标题查询公告")
    @PostMapping("/findAllNoticeBytitle")
    public void findAllNoticeBytitle(HttpServletRequest request, HttpServletResponse response,@RequestParam("title") String title, @RequestParam("size") int size, @RequestParam("current") int current) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.like("title",title);
        Page<YNotice> shUserPage = new Page<>(current, size);
        Page<YNotice> shUserPages = yNoticeMapper.selectPage(shUserPage, wrapper);
        List<YNotice> records = shUserPages.getRecords();
        int total = yNoticeMapper.selectCount(wrapper);
        if (records.size() > 0) {
            Result ok = Result.ok().data("notice", records).data("total", total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
}

